﻿using Kiva7.Settings;

namespace Kiva7.ViewModels
{
    public class DataController
    {
        private static string _lenderId = "";

        private static readonly object _MainViewModelLock = new object();

        private static readonly MainViewModel _ViewModel = new MainViewModel();
        public static MainViewModel ViewModel
        {
            get
            {
                CheckLenderId();
                return _ViewModel;
            }
        }

        public static void RefreshData()
        {
            lock (_MainViewModelLock)
            {
                _lenderId = AppSettings.Current.LenderIdSetting;
                _ViewModel.Reload(_lenderId);
            }

        }

        public static void CheckLenderId()
        {
            if (_lenderId != AppSettings.Current.LenderIdSetting)
            {
                lock (_MainViewModelLock)
                {
                    if (_lenderId != AppSettings.Current.LenderIdSetting)
                    {
                        _lenderId = AppSettings.Current.LenderIdSetting;
                        _ViewModel.Reload(_lenderId);
                    }
                }
            }
        }
    }
}
